module.exports = function (mongoose, Schema, ObjectId) {
  let userSchema = new Schema({
    username: {
      type: String,
      required: [true, '缺少用户名。'],
      validate: {
        validator: (v) => /^[0-9a-zA-Z_]{6,20}$/.test(v),
        message: '用户名格式不正确。'
      }
    },
    password: {
      type: String,
      required: [true, '缺少密码。']
    },
    email: {
      type: String,
      required: [true, '缺少电子邮箱。'],
      validate: {
        validator: (v) => /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(v),
        message: '电子邮箱格式不正确。'
      }
    },
    emailVerified: {
      type: Boolean,
      default: false
    },
    blog: {
      type: ObjectId,
      ref: 'Blog'
    },
    createdAt: {
      type: Date,
      default: Date.now
    },
    LastLoginedAt: {
      type: Date,
      default: null
    }
  })

  userSchema.static('findByUsername', function (username) {
    return this.findOne({username})
  })

  userSchema.static('findByEmail', function (email) {
    return this.findOne({email})
  })

  return mongoose.model('User', userSchema)
}
